package com.gzyj.jtt808.v2011.protocol;

import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.Package;
import com.gzyj.jtt808.v2011.enums.BusinessDataType;
import com.gzyj.jtt808.v2011.enums.CarColor;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;
import java.nio.charset.Charset;

/**
 * @author lhj
 */
@Data
public class JTT808_0x0100 extends Package implements Serializable {
    private int AreaID;//标示终端安装车辆所在的省域，0 保留，由平台取默认值。省域 ID 采用 GB/T 2260 中规定的行政区划代码六位中前两位
    private int CityOrCountyId;//市县域 ID 标示终端安装车辆所在的市域和县域，0 保留，由平台取默认值。市县域 ID 采用 GB/T 2260 中规定的行政区划代码六位中后四位。
    private String MakerId;//制造商 ID
    private String TerminalModel;// 8个字节  ，此终端型号由制造商自行定义，位数不足时，后补“0X00”
    private String TerminalId;//终端 ID
    private CarColor PlateColor;//车牌颜色
    private String PlateNo;//车辆标识 车牌颜色为 0 时，表示车辆 VIN；否则，表示公安交通管理部门颁发的机动车号牌。

    public JTT808_0x0100() {
        super(BusinessDataType.终端注册.getCode());
    }

    @Override
    public void encodeImpl(ByteBuf buf) {
        buf.writeShort(this.AreaID);
        buf.writeShort(this.CityOrCountyId);
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.MakerId, 5));
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.TerminalModel, 8));
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.TerminalId, 7));
        buf.writeByte(this.PlateColor.getCode());
        buf.writeBytes(this.PlateNo.getBytes(Charset.forName("GBK")));
    }

    @Override
    public JTT808_0x0100 decodeImpl(ByteBuf buf) {
        byte[] temp = HexBytesUtil.readByOffset(buf, 2);
        this.AreaID = Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16);
        temp = HexBytesUtil.readByOffset(buf, 2);
        this.CityOrCountyId = Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16);
        temp = HexBytesUtil.readByOffset(buf, 5);
        this.MakerId=HexBytesUtil.parseString(temp);
        temp = HexBytesUtil.readByOffset(buf, 8);
        this.TerminalModel=new String(temp);
        temp = HexBytesUtil.readByOffset(buf, 7);
        this.TerminalId=HexBytesUtil.parseString(temp);
        temp = HexBytesUtil.readByOffset(buf, 1);
        int code=Integer.parseInt(HexBytesUtil.bytesToHex(temp),16);
        this.PlateColor=CarColor.find(code);
        temp=HexBytesUtil.readByOffset(buf,buf.readableBytes());
        this.PlateNo=new String(temp,Charset.forName("GBK"));


        return this;
    }
}
